/**
 * 自动创建的对应js目录
 * 创建时间：2018-07-23 12:25:31
 * 技术支持：曹传喜
 * 邮箱：15075107120@139.com
 */
$(function () {
    initDatagrid();
});

// 表格内容初始化
function initDatagrid() {
    var url = $.util.getRequestJson('/json/childAccount.childList.json');

    $('#listTable').datagrid({
        url: url,
        idField: 'id',
        fit: true,
        pageSize: 20,
        pagination: true,
        checkOnSelect: false,
        selectOnCheck: false,
        singleSelect: true,
        toolbar: '#toolbarButton',
        fitColumns: true,
        columns: [
            [
                {
                    field: 'name',
                    title: '名称',
                    width: 12
                },
                {
                    field: 'account',
                    title: '账号',
                    align: 'center',
                    width: 8
                },
                {
                    field: 'createTime',
                    title: '创建时间',
                    align: 'center',
                    width: 12
                },
                {
                    field: 'status',
                    title: '状态',
                    align: 'center',
                    width: 8,
                    formatter: function (value, row) {
                        return value == 0 ? '停用' : '启用';
                    }
                },
                {
                    field: 'remark',
                    title: '备注',
                    width: 18
                }, {
                field: 'id',
                title: '操作',
                align: 'center',
                width: 18,
                formatter: function (value, row) {
                    var aEl = '<a href=javascript:showDetail("{0}"); class="easyui-tooltip " title="详情">详情</a>';
                    aEl += ' | <a href=javascript:showEdit("{0}"); class="easyui-tooltip " title="详情">编辑</a>';
                    if (row.status == 1) {
                        aEl += ' | <a href=javascript:updateStatus("{0}",0); class="easyui-tooltip " title="详情">禁用</a>';
                    } else {
                        aEl += ' | <a href=javascript:updateStatus("{0}",1); class="easyui-tooltip " title="详情">启用</a>';
                    }
                    aEl += ' | <a href=javascript:updateStatus("{0}",-1); class="easyui-tooltip " title="详情">删除</a>';
                    //格式化操作按钮
                    return $.string.format(aEl, row.id);
                }
            }
            ]
        ]
    });
}

function updateStatus(id, status) {
    var msg = '确定要删除吗？';
    if (status == 1) {
        msg = '确定要启用吗？';
    } else if (status == 0) {
        msg = '确定要禁用吗？';
    }
    $.easyui.messager.confirm(msg, function (r) {
        if (r) {
            var url = $.util.getRequestJson('/json/childAccount.updateStatus.json');
            var param = {id: id, status: status};
            $.util.post(url, param, function (json) {
                if (json.success) {
                    $('#listTable').datagrid('reload');
                }
                $.easyui.messager.show(json.message);
            });
        }
    });
}

function showEdit(id) {
    var title = id ? '编辑子账号' : '添加子账号';
    var url = $.util.getRootPath() + '/page/childAccount/edit.html';
    url = id ? url + '?id=' + id : url;
    var dialog = $.easyui.showDialog({
        title: title,
        width: 500,
        height: 320,
        maximizable: true,
        href: url,
        topMost: true,
        saveButtonText: '保存',
        onSave: function () {
            subForm(dialog);
        }
    });
}

function subForm(dialog) {
    var url = $.util.getRootPath() + '/childAccount/save';
    var form = parent.$('#childAccountForm').form('submit', {
        url: url,
        onSubmit: function () {
            return parent.$('#childAccountForm').form('validate');
        },
        success: function (data) {
            if (data.success) {
                $('#listTable').datagrid('reload');
                dialog.dialog('close');
            }
            $.easyui.messager.show(data.message);
        }
    });
}

function showDetail(id) {
    var title = "详情";
    var url = $.util.getRootPath() + '/page/childAccount/detail.html';
    url = url + '?id=' + id;
    var dialog = $.easyui.showDialog({
        iconCls: "",
        title: title,
        width: 500,
        height: 280,
        maximizable: true,
        href: url,
        topMost: true,
        enableSaveButton: false,
        closeButtonText: '关闭'
    });
}